package com.gzy.literature.dao;

import com.gzy.literature.entity.UserRecommendations;
import org.apache.ibatis.annotations.*;
import org.springframework.data.domain.Pageable;
import java.util.List;


public interface UserRecommendationsDao {

    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象，如果未找到则返回null
     */
    @Select("SELECT * FROM user_recommendations WHERE user_id = #{userId}")
    @Results(id = "userRecommendationsResultMap", value = {
            @Result(property = "userId", column = "user_id"),
            @Result(property = "workId", column = "work_id"),
            @Result(property = "recommendationTime", column = "recommendation_time")
    })
    UserRecommendations queryById(@Param("userId") Integer userId);

    /**
     * 分页查询指定条件的数据
     *
     * @param userRecommendations 查询条件（可以为null或包含部分字段用于筛选）
     * @return 对象列表
     */
    @Select("<script>" +
            "SELECT * FROM user_recommendations WHERE 1=1 " +
            "<if test='userRecommendations != null'>" +
            "<if test='userRecommendations.workId != null'> AND work_id = #{userRecommendations.workId} </if>" +
            "</if>" +
            "</script>")
    @Results(id = "userRecommendationsResultMapList", value = {
            @Result(property = "userId", column = "user_id"),
            @Result(property = "workId", column = "work_id"),
            @Result(property = "recommendationTime", column = "recommendation_time")
    })
    List<UserRecommendations> queryCll(@Param("userRecommendations") UserRecommendations userRecommendations);

    /**
     * 统计总行数（用于分页）
     *
     * @param userRecommendations 查询条件
     * @return 总行数
     */
    @Select("<script>" +
            "SELECT COUNT(*) FROM user_recommendations WHERE 1=1 " +
            "<if test='userRecommendations != null'>" +
            "<if test='userRecommendations.workId != null'> AND work_id = #{userRecommendations.workId} </if>" +
            "</if>" +
            "</script>")
    long count(@Param("userRecommendations") UserRecommendations userRecommendations);

    /**
     * 新增数据
     *
     * @param userRecommendations 实例对象
     * @return 影响行数
     */
    @Insert("INSERT INTO user_recommendations (user_id, work_id, recommendation_time) VALUES (#{userId}, #{workId}, #{recommendationTime})")
    @Options(useGeneratedKeys = true, keyProperty = "userId", keyColumn = "user_id")
    int insert(UserRecommendations userRecommendations);

    /**
     * 修改数据
     *
     * @param userRecommendations 实例对象（包含要更新的字段）
     * @return 影响行数
     */
    @Update("<script>" +
            "UPDATE user_recommendations " +
            "<set>" +
            "<if test='userRecommendations.workId != null'> work_id = #{userRecommendations.workId}, </if>" +
            "<if test='userRecommendations.recommendationTime != null'> recommendation_time = #{userRecommendations.recommendationTime}, </if>" +
            "</set>" +
            "WHERE user_id = #{userRecommendations.userId}" +
            "</script>")
    int update(UserRecommendations userRecommendations);

    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 影响行数
     */
    @Delete("DELETE FROM user_recommendations WHERE user_id = #{userId}")
    int deleteById(@Param("userId") Integer userId);
}